import java.io.*;
import java.util.Hashtable;
import java.util.Scanner;

import java.util.StringTokenizer;
//import java.util.Enumeration;

 
// author Sandeep and Hemant
// Rtype Instructions include :
   // add,sub
// Itype Instructions include :
  // load,store,bez,addi
// add   rd, rs, rt      100000
// sub   rd, rs, rt      100010
// load r1, 0(r2)        100011
// store r1, 0(r2)       100100
// bez r1, done          100110 branch if register is equal to zero      
// addi r1, r0, imm      100111  


public class Assembler {

        //static String[] rtype ={"add","sub"};
        //static String[] itype ={"load","store","bez","addi"};
       
        public static String fillString(char filler, int count){
        char[] Cary = new char[count];
        while(count>0){ count--;  Cary[count] = filler ;   }
        return new String(Cary);
        }
       
    @SuppressWarnings("unchecked")
        public static void main(String[] args) throws IOException {
        Scanner s = null;
        int m = 0;
        int i = 0;
       
        String[] strlines = new String[100]; // Considering the string to be less than 100 lines
        String[] hash1 = new String[50];
        String[][] Final = new String[50][50];
        String[] label = new String [50] ;  
        String[] jumpaddr = new String[50];
       
       
     // Forming the Key Value pairs by using a Hasp-map, we can also use a Hash-table here!!      
          Hashtable<String, String> ht = new Hashtable<String,String>();
         
      // forming the Key value pairs for the Instructions both R-type and I-type
     
      ht.put("add","100000");
      ht.put("sub","100010");
      ht.put("load","100011");
      ht.put("Store","100100");
      ht.put("bez","100110");
      ht.put("addi","100111");
      // forming the key value pairs for the registers
     
      ht.put("r0", "00000");
      ht.put("r1", "00001");
      ht.put("r2", "00010");
      ht.put("r3", "00011");
      ht.put("r4", "00100");
      ht.put("r5", "00101");
      ht.put("r6", "00110");
      ht.put("r7", "00111");
      ht.put("r8", "01000");
      ht.put("r9", "01001");
      ht.put("r10", "01010");
      ht.put("r11", "01011");
      ht.put("r12", "01100");
      ht.put("r13", "01101");
      ht.put("r14", "01110");
      ht.put("r15", "01111");
      ht.put("r16", "10000");
      ht.put("r17", "10001");
      ht.put("r18", "10010");
      ht.put("r19", "10011");
      ht.put("r20", "10100");
      ht.put("r21", "10101");
      ht.put("r22", "10110");
      ht.put("r23", "10111");
      ht.put("r24", "11000");
      ht.put("r25", "11001");
      ht.put("r26", "11010");
      ht.put("r27", "11011");
      ht.put("r28", "11100");
      ht.put("r29", "11101");
      ht.put("r30", "11110");
      ht.put("r31", "11111");

		System.out.println(args[0]);
      BufferedReader obj = null ;
      try {

          obj = new BufferedReader(new FileReader(args[0]));
          String line = null;
         
         
          while ((line = obj.readLine()) != null) {
                      i++;
                     strlines[i] = line ;
                   }
                   
                                     
      } catch (IOException x) {
          System.err.println(x);
      } finally {
          if (obj != null) {
              obj.close();
          }
      }
     
      for(int n=1; n<=i;n++)
      {
      String temp = strlines[n].replace(',', ' ');
      strlines[n] = temp ;
      //System.out.println(strlines[n]);
      }
     
       for(int n=1; n<=i; n++)
        {   s = new Scanner(strlines[n]);
            int j=1;
             while(s.hasNext())
              {
              Final[n][j] = s.next();
              j++;                    
              }
        }      
     
     for (int n=1; n<=i; n++)
       { int j=1;
       String bez = "bez" ;
         if(Final[n][1].equals(bez))
          {
         
            m++;
           while(Final[n][j]!= null)
             { j++; }
           label[m] = Final[n][j-1];
           //System.out.println(m);
           }
        }
         
         
       
           
      for (int k=1; k<=m; k++)    
       {
        for (int n=1; n<=i; n++)
               
         {int j=1; String lab = label[k] + ":" ;
         
          while(Final[n][j]!= null)
            {
                   
              if(Final[n][j].equals(lab))
             
                {  
                 
                  String temp = (Integer.toBinaryString(n)) ;
                 
                   jumpaddr[k] = fillString('0',8-temp.length()) + temp;
                   //System.out.println(jumpaddr[k]);
                   
                  Final[n][j]= " ";  
                }
                j++;
          }      
         }  
       }
       
      for (int p=1; p<=m; p++)
             
        {  int k=0;
           for (int n=1; n<=i; n++)
           { int j=1;
           String bez = "bez" ;
              if(Final[n][1].equals(bez))
               { k++;
                while(Final[n][j]!= null)
                 { j++; }
                  Final[n][j-1] = jumpaddr[k];
                  ht.put(Final[n][j-1],Final[n][j-1]);
                  System.out.println(Final[n][j-1]) ;
                }
               
           }
        }
       
     
      for(int n=1; n<=i; n++)
        { int j=1;
           String load = "load" ;
           if(Final[n][1].equals(load))            
            {  while(Final[n][j]!=null)      
                  { j++; }    
             
              for(int r=0; r<Final[n][j-1].length();r++ )
              {  
                if(Final[n][j-1].charAt(r)=='(')
                 {  String no = Final[n][j-1].substring(0,r);
                    //System.out.println("the value of no"+ no);
                    String reg = Final[n][j-1].substring(r+1,Final[n][j-1].length()-1) ;
                    Final[n][j-1] = reg ;
                    //System.out.println("the value of reg"+ reg);
                      int temp = Integer.parseInt(no);
                      //System.out.println("the value of temp"+ temp);
                    String offset = Integer.toBinaryString(temp);
                    //System.out.println("the value of offset"+ offset);
                     offset = fillString('0',16-offset.length()) + offset ;
                    Final[n][j] = offset;
                    ht.put(Final[n][j],Final[n][j]);
                  }
               }  
             }
          }  
     
       for(int n=1; n<=i; n++)
       {   int q = 1;
           String addi = "addi" ;
       if(Final[n][1].equals(addi))
       { while(Final[n][q]!=null)      
            { q++; }
       String noimm = Final[n][q-1];
       int temp = Integer.parseInt(noimm);
       String immvalue = Integer.toBinaryString(temp);
       System.out.println("the value of immidiate value"+ immvalue);  
       immvalue = fillString('0',16-immvalue.length()) + immvalue ;
       Final[n][q] = immvalue;
       ht.put(Final[n][q],Final[n][q]);
       }
       
       }    
        for(int n=1; n<=i; n++)
        { int j=1;
          while(Final[n][j]!=null)
           {  System.out.println("Final[" + n +"][" +j+ "]" +   ht.get(Final[n][j]));
               j++;
           }
        }
               
        for(int n=1; n<=i; n++)
        { int j=1;
          String line = "";
          String k ="";
          while(Final[n][j]!=null)
           {      
             if(ht.get(Final[n][j])!=null)
               k = ht.get(Final[n][j]) ;
              else
              k="";
               
          line = line + k;
               j++;
           }      
               
             
             line = line + fillString('0',32-line.length()) ;
             
          System.out.println(line) ;
         
           
        }        
               
}
   
}